package com.ytjj.qmyx.admin.controller.channelLabel;


import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.model.request.channel.ChannelLabelRequest;
import com.ytjj.qmyx.admin.model.request.channel.ChannelProductLabelRequest;
import com.ytjj.qmyx.admin.service.channelLabel.ChannelLabelService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 渠道标签管理
 */
@RestController
@RequestMapping("/channelLaber")
public class ChannelLabeControllerl {

    @Autowired
    private ChannelLabelService channelLabelService;

    /**
     * 标签列表
     * @param channelLabelRequest
     * @return
     */
    @PostMapping("/findList")
    public CommonResult findList(@RequestBody ChannelLabelRequest channelLabelRequest){
        Assert.isTrue(StringUtils.isNotBlank(channelLabelRequest.getChannel()),"渠道必须传");
        return CommonResult.success(channelLabelService.findList(channelLabelRequest));
    }

    /**
     * 保存标签
     * @param request
     * @return
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody ChannelLabelRequest request){
        Assert.isTrue(StringUtils.isNotBlank(request.getChannel()),"渠道不能为空");
        Assert.notNull(request.getCategory(),"类别不能为空");
        Assert.isTrue(StringUtils.isNotBlank(request.getName()),"名称不能为空");
        Assert.notNull(request.getType(),"类型不能为空");
        channelLabelService.save(request);
        return CommonResult.success();
    }

    /**
     * 修改标签详细
     * @param request
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody ChannelLabelRequest request){
        Assert.notNull(request.getId(),"id必须传");
        channelLabelService.update(request);
        return CommonResult.success();
    }

    /**
     * 查询标签详细
     * @param id
     * @return
     */
    @GetMapping("/findDetail/{id}")
    public CommonResult findDetail(@PathVariable Integer id){
        Assert.notNull(id,"id必须传");
        return CommonResult.success(channelLabelService.findDetail(id));
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        Assert.notNull(id,"id必须传");
        channelLabelService.delete(id);
        return CommonResult.success();
    }

    /**
     * 渠道商品批量打标签
     * @param request
     * @return
     */
    @PostMapping("/setProductLabel")
    public CommonResult setProductLabel(@RequestBody ChannelProductLabelRequest request){
        Assert.notNull(request.getChannel(),"渠道不能为空");
        Assert.isTrue(CollectionUtils.isNotEmpty(request.getProductIds()),"产品id不能为空");
        Assert.isTrue(CollectionUtils.isNotEmpty(request.getLabelIds()),"标签id不能为空");
        channelLabelService.setProductLabel(request);
        return CommonResult.success();
    }

    /**
     * 渠道批量删标签
     * @param request
     * @return
     */
    @PostMapping("/delProductLabel")
    public CommonResult delProductLabel(@RequestBody ChannelProductLabelRequest request){
        Assert.notNull(request.getChannel(),"渠道不能为空");
        Assert.isTrue(CollectionUtils.isNotEmpty(request.getProductIds()),"商品id不能为空");
        channelLabelService.delProductLabel(request);
        return CommonResult.success();
    }

    @GetMapping("/testAutoLabel/{productId}")
    public CommonResult testAutoLabel(@PathVariable Integer productId){
        channelLabelService.testAutoLabel(productId);
        return CommonResult.success();
    }

}
